<!--<div class="container">-->

    <form class="form-horizontal" id="post" method="post" action="<?=base_url('object_attributes/submit_post')?>" name="post">

        <input type="hidden" value="NEW" name="post_action">
        <input type="hidden" value="villa" name="type" id="type">

        <fieldset>

            <div class="group">

                <span class="group-title">SELECT ATTRIBUTES</span>             

                <div class="control-group">

                    <label class="control-label smaller">Attribute:</label>

                    <div class="controls docs-input-sizes">

                        <select class="span3" name="attributes_ID" id="attributes_ID" tabindex="1">

                            <option value="0">--Please Select</option>
                            <?foreach ($attributes as $attribute) :?>

                                <option value="<?=$attribute['ID']?>"><?=$attribute['name']?></option>

                                <?endforeach;?>

                        </select>

                        <a class="btn btn-mini" title="Add Attribute" id="new_attribute" onclick="bpopup({url: base_url+'attributes/view_new_from_object/'+jQuery('#type').val()});return false;"><i class="icon-plus"></i></a>
                        
                    </div>

                </div>

                <?=info('From drop down list, select an attribute you want to add to the group,  and click on Save changes. To enter a new attribute, click on the plus icon next to the search box.')?>        

            </div>

            <div id="validation_error"></div> 

            <div class="form-actions">

                <img id="ajax-loading" class="ajax-loading" src="<?=base_url()?>assets/img/backgnds/loading.gif" style="visibility: hidden;">

                <button class="btn btn-primary" type="submit" tabindex="4">Save changes</button>
                <button class="btn" tabindex="5" id="cancel">Cancel</button>


            </div>

        </fieldset>

    </form>

<!--</div>-->

<script>

    jQuery(document).ready(function() {       

            //check dropdown methot - valdation
            $.validator.addMethod("check_item_dropdown", function(value, element) {  
                    return this.optional(element) || value != 0  ;   
                }, "Please select an item from the dropdown list.");

            // Override default error message
            jQuery.validator.messages.required = "";   

            // Override generation of error label
            jQuery("#post").validate({

                    rules: {

                        //General
                        value: { check_item_dropdown: true },
                        name: "required"

                    }, 

                    highlight: function(element) {

                        //console.log(element.name)
                        $(element).parent().parent().addClass("error");

                    },
                    unhighlight: function(element) {

                        $(element).parent().parent().removeClass("error");

                    },

                    submitHandler: function(form) {

                        //$('button[type=submit]').attr('disabled', 'disabled');

                        $('#ajax-loading').css('visibility','visible');

                        var form = $("#post");
                        var form_data = form.serialize();

                        $.ajax({
                                url: form.attr('action'),
                                dataType: 'json',
                                type: "POST",
                                data: form_data,
                                success: function (data, textStatus, xhr) {

                                    jQuery('#ajax-loading').css('visibility','hidden');

                                    if(data.action==true){

                                        jQuery('.form-actions').append('&nbsp;&nbsp;&nbsp;'+data.msg);

                                        // REDIRECT
                                        function promo_show(){
                                            window.location.href = base_url+'<?=$page?>/view_all'; 
                                        }
                                        //window.setTimeout(function() { promo_show(); }, 1001);

                                    }else{

                                        $('button[type=submit]').removeAttr('disabled');
                                        $('#validation_error').html(error(data.msg,'error'));
                                    }
                                },
                                error: function (xhr, textStatus, errorThrown) {
                                    alert('Error ocured...\n'+xhr+'\n'+textStatus+errorThrown+'\n');
                                    $('button[type=submit]').removeAttr('disabled'); 
                                    $('#ajax-loading').css('visibility','hidden');
                                }
                        });

                    },
                    errorPlacement: function(error, element){}
            });

            //helper function - alert
            function error(string,type)
            {
                html = '<div class="alert alert-'+type+' clearfix">'
                '<button type="button" class="close" data-dismiss="alert">&times;</button>'
                html+= string; 
                html+= '</div>';
                return html;  
            }

            // enable form submit button
            $('button[type=submit]').removeAttr('disabled'); 

            //cancel button
            $('#cancel').live('click',function(){
                    window.location.href = base_url+'<?=$page?>/view_all'; 
                    return false;
            })

    });

</script>
